package com.example.app.controller.category;

import com.example.app.domain.category.CategoryItemVO;
import com.example.app.domain.category.CategoryListVO;
import com.example.app.domain.category.CategoryTreeVO;
import com.example.module.module.category.entity.Category;
import com.example.module.module.category.service.CategoryService;
import com.example.module.utils.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/game")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @GetMapping("/category")
    public Response getCategoryList() {

        List<Category> categories = categoryService.findAll();

        List<CategoryTreeVO> tree = categories.stream()
                .filter(c -> c.getParentId() == null) // 筛选父分类
                .map(parent -> {
                    // 创建父节点 VO
                    CategoryTreeVO parentVO = new CategoryTreeVO();
                    parentVO.setId(parent.getId());
                    parentVO.setImage(parent.getImage());
                    parentVO.setName(parent.getName());


                    // 收集子分类并映射为 ItemVO
                    List<CategoryItemVO> children = categories.stream()
                            .filter(child -> parent.getId().equals(child.getParentId()))
                            .map(child -> new CategoryItemVO(
                                    child.getId(),
                                    child.getImage(),
                                    child.getName()
                            ))
                            .collect(Collectors.toList());

                    parentVO.setChildren(children);
                    return parentVO;
                })
                .collect(Collectors.toList());

        CategoryListVO categoryListVO = new CategoryListVO();
        categoryListVO.setList(tree);

        return new Response(1001, categoryListVO);
    }

    @GetMapping("/third_level_category")
    public Response getThirdLevelCategory(@RequestParam Long parentId,
                                          @RequestParam(required = false) Long selectedId) {
        List<Category> categories = categoryService.findAll();

        //筛选三级类目
        List<Map<String,Object>> thirdLevelCategories = categories.stream()
                .filter(c ->parentId.equals(c.getParentId()))
                .map(c -> {
                    Map<String,Object> categoryMap = new HashMap<>();
                    categoryMap.put("id",c.getId());
                    categoryMap.put("name", c.getName());
                    categoryMap.put("image", c.getImage());
                    return categoryMap;
                })
                .collect(Collectors.toList());

        // 获取选中类目的详细信息
        Map<String, Object> selectedCategory = new HashMap<>();
        if (selectedId != null) {
            Optional<Category> optionalCategory = categories.stream()
                    .filter(c -> c.getId().equals(selectedId))
                    .findFirst();
            if (optionalCategory.isPresent()) {
                Category category = optionalCategory.get();
                selectedCategory.put("id", category.getId());
                selectedCategory.put("name", category.getName());
                selectedCategory.put("image", category.getImage());
            }
        } else if (!thirdLevelCategories.isEmpty()) {
            // 如果没有指定 selectedId，默认选中第一个类目
            Map<String, Object> firstCategory = thirdLevelCategories.get(0);
            selectedCategory.put("id", firstCategory.get("id"));
            selectedCategory.put("name", firstCategory.get("name"));
            selectedCategory.put("image", firstCategory.get("image"));
        }

        // 构建响应
        Map<String, Object> orderedResponse = new LinkedHashMap<>();
        orderedResponse.put("thirdLevelCategories", thirdLevelCategories);
        orderedResponse.put("categoryContent", selectedCategory);

        return new Response(1001, orderedResponse);
    }
}
